/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package byblos.apresentacao;

import byblos.negocio.NegocioExcecao;
import byblos.negocio.reserva.Reserva;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.joda.time.DateTime;

/**
 *
 * @author marano
 */
public class ServletEnviarVisanet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    private void fazerLogica(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Map parametros = request.getParameterMap();

        Reserva reserva = (Reserva) parametros.get("reserva");

        if (reserva == null) {
            throw new NegocioExcecao("Não foi possível deinir a reserva!");
        }

        parametros.put("identificacao", "2046596");
        parametros.put("meiopag", "VISANET");
        parametros.put("ambiente", "teste");
        parametros.put("modulo", "VISAVBV");
        parametros.put("operacao", "Pagamento");
        parametros.put("tid", tid("1023289455"));
        parametros.put("orderid", "" + reserva.getId());
        parametros.put("order", "Reserva Byblos");
        parametros.put("price", "" + reserva.getValor());
        parametros.put("damount", "" + reserva.getValorMoeda().toString());

        response.sendRedirect("http://comercio.locaweb.com.br/comercio.comp");
    }

    public static String tid(String lojaVisa) {
        StringBuilder tidString = new StringBuilder();

        tidString.append(lojaVisa.substring(4, 9));

        DateTime d = new DateTime();
        System.out.println("loja visa formatado "+lojaVisa.substring(4, 9));
        System.out.println("ultimo digito ano "+(""+d.getYearOfEra()).substring(3, 4));
        System.out.println("dia atual corrente "+preencherComZero(d.getDayOfYear(), 3));
        System.out.println("hora "+preencherComZero(d.getHourOfDay(), 2));
        System.out.println("minuto "+preencherComZero(d.getMinuteOfHour(), 2));
        System.out.println("segundo "+preencherComZero(d.getSecondOfMinute(), 2));
        System.out.println("decimo de segundo "+(""+d.getMillisOfSecond()).substring(0, 1));
        System.out.println("tipo de transacao 1001");
        tidString.append((""+d.getYearOfEra()).substring(3, 4));
        tidString.append(preencherComZero(d.getDayOfYear(), 3));
        tidString.append(preencherComZero(d.getHourOfDay(), 2));
        tidString.append(preencherComZero(d.getMinuteOfHour(), 2));
        tidString.append(preencherComZero(d.getSecondOfMinute(), 2));
        tidString.append((""+d.getMillisOfSecond()).substring(0, 1));

        tidString.append("1001");
        System.out.println("============tid da locaweb: " + tidString.toString());
        return tidString.toString();
    }

    private static String preencherComZero(int numero, int quantidadeAlgarismo) {
        String string = "" + numero;
        while (string.length() < quantidadeAlgarismo) {
            string = "0" + string;
        }
        return string;
    }
    
    public static void main(String[] args) {
        tid("1023289455");
    }

    // <editor-fold defaultstate="collapsed" desc="Métodos HttpServlet. Clique no sinal de + à esquerda para editar o código.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        fazerLogica(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        fazerLogica(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
